package com.dev.module_ui_composable.components

import android.content.res.Configuration
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Info
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import kotlinx.coroutines.delay

@Composable
fun AlertDialogWithButtons(
    openAlertDialog: Boolean,
    dialogTitle: String,
    dialogText: String,
    icon: @Composable (() -> Unit)? = null,
    onDismissRequest: () -> Unit,
    onConfirmation: () -> Unit
) {
    if (!openAlertDialog) return
    AlertDialog(
        containerColor = Color.White,
        onDismissRequest = {
            onDismissRequest()
        },
        confirmButton = {
            TextButton(onClick = { onConfirmation() }) {
                Text(text = "确认", fontSize = 18.sp)
            }
        },
        dismissButton = {
            TextButton(
                modifier = Modifier.padding(end = 30.dp),
                onClick = { onDismissRequest() }
            ) {
                Text(text = "取消", fontSize = 18.sp)
            }
        },
        icon = icon,
        title = {
            Text(text = dialogTitle)
        },
        text = {
            Text(text = dialogText)
        }
    )
}

@Composable
fun IndicatorDialog(
    openDialog: Boolean,
    dialogText: String,
    modifier: Modifier = Modifier,
    onDismissRequest: () -> Unit = {}
) {
    if (!openDialog) return
    Dialog(
        onDismissRequest = { onDismissRequest() }
    ) {
        Card(
            modifier = modifier
                .wrapContentSize()
                .padding(16.dp),
            colors = CardDefaults.cardColors(MaterialTheme.colorScheme.surface)
        ) {
            Column(
                modifier = Modifier.size(135.dp),
                verticalArrangement = Arrangement.SpaceEvenly,
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                CircularProgressIndicator(
                    modifier = Modifier
                        .size(50.dp)
                        .padding(5.dp),
                    color = MaterialTheme.colorScheme.primary,
                    trackColor = MaterialTheme.colorScheme.inverseOnSurface
                )

                Text(
                    text = dialogText,
                    modifier = Modifier.padding(5.dp),
                    style = MaterialTheme.typography.bodyMedium,
                    textAlign = TextAlign.Center
                )
            }
        }
    }
}

@Composable
fun DialogWithButtons(
    openDialog: Boolean,
    modifier: Modifier = Modifier,
    dialogText: String = "确认选择",
    onConfirmation: () -> Unit,
    onDismissRequest: () -> Unit = {}
) {
    if (!openDialog) return
    Dialog(
        onDismissRequest = { onDismissRequest() }
    ) {
        Card(
            modifier = modifier
                .wrapContentSize()
                .padding(16.dp),
            colors = CardDefaults.cardColors(Color.White)
        ) {
            Column(
                modifier = Modifier
                    .width(280.dp)
                    .height(180.dp),
                verticalArrangement = Arrangement.SpaceEvenly,
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                Text(
                    text = dialogText,
                    modifier = Modifier.padding(5.dp),
                    style = MaterialTheme.typography.headlineSmall,
                    textAlign = TextAlign.Center
                )

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(30.dp, Alignment.CenterHorizontally)
                ) {
                    FilledTonalButton(
                        onClick = { onDismissRequest() },
                        modifier = Modifier.width(100.dp),
                        colors = ButtonDefaults.filledTonalButtonColors(Color(0xFFF3F2F2))
                    ) {
                        Text(text = "取消", color = Color.Black)
                    }
                    FilledTonalButton(
                        onClick = { onConfirmation() },
                        modifier = Modifier.width(100.dp),
                        colors = ButtonDefaults.filledTonalButtonColors(Color(0xFFDA5255))
                    ) {
                        Text(text = "确认", color = Color.White)
                    }
                }
            }
        }
    }
}

@Composable
fun ProgressIndicatorDialog(
    openDialog: Boolean,
    progress: Float,
    baseDialogText: String,
    modifier: Modifier = Modifier,
    onDismissRequest: () -> Unit = {}
) {
    var dots by remember { mutableStateOf("") }

    LaunchedEffect(Unit) {
        var dotsCount = 0
        while (true) {
            delay(1000)  // 更新间隔
            dotsCount = (dotsCount % 3) + 1
            dots = ".".repeat(dotsCount)
        }
    }

    if (!openDialog) return
    Dialog(
        onDismissRequest = { onDismissRequest() }
    ) {
        Card(
            modifier = modifier
                .wrapContentSize()
                .padding(16.dp),
            colors = CardDefaults.cardColors(MaterialTheme.colorScheme.surface)
        ) {
            Column(
                modifier = Modifier.size(200.dp),
                verticalArrangement = Arrangement.SpaceEvenly,
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                ProgressIndicator(progress = progress)

                Row(
                    modifier = Modifier.padding(5.dp),
                    verticalAlignment = Alignment.CenterVertically,
                ) {
                    Text(
                        text = baseDialogText,
                        style = MaterialTheme.typography.titleMedium,
                        textAlign = TextAlign.Center
                    )
                    Text(
                        text = dots,
                        style = MaterialTheme.typography.titleMedium,
                        textAlign = TextAlign.Center
                    )
                }
            }
        }
    }
}

@ExperimentalMaterial3Api
@Preview(
    name = "Light Mode",
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    locale = "zh-rCN",
    showBackground = true,
    device = "spec:width=1440px,height=3200px,dpi=560"
)
@Composable
private fun AlertDialogWithButtonsPreview() {
    AlertDialogWithButtons(
        openAlertDialog = true,
        dialogTitle = "对话框顶部标题",
        dialogText = "Dialog 组件可在主应用内容上方的一层显示弹出消息或请求用户输入。它会打造干扰性界面体验，以吸引用户的注意力",
        icon = {
            Icon(Icons.Rounded.Info, contentDescription = null)
        },
        onDismissRequest = {},
        onConfirmation = {}
    )
}

@Preview(
    name = "Light Mode",
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    locale = "zh-rCN",
    showBackground = true,
    device = "spec:width=1440px,height=3200px,dpi=560"
)
@Composable
private fun MinimalDialogPreview() {
    IndicatorDialog(
        openDialog = true,
        dialogText = "正在保存添加...",
        onDismissRequest = {}
    )
}

@Preview(
    name = "Light Mode",
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    locale = "zh-rCN",
    showBackground = true,
    device = "spec:width=1440px,height=3200px,dpi=560"
)
@Composable
private fun DialogWithButtonsPreview() {
    DialogWithButtons(
        openDialog = true,
        dialogText = "确认选择",
        onConfirmation = {}
    )
}

@Preview(
    name = "Light Mode",
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    locale = "zh-rCN",
    showBackground = true,
    device = "spec:width=1440px,height=3200px,dpi=560"
)
@Composable
private fun ProgressDialogPreview() {
    ProgressIndicatorDialog(
        openDialog = true,
        progress = 0.7f,
        baseDialogText = "正在转换中",
        onDismissRequest = {}
    )
}